package com.wan.data.redis.jedis;

import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public enum JedisMode {

    /** 默认单节点 */
    standalone{
        @Override
        public UnifiedJedis createJedis(JedisSetting jedisSetting) {
            List<HostAndPort> hostAndPorts = jedisSetting.getHostAndPorts();
            Optional<HostAndPort> first = hostAndPorts == null ? Optional.empty() : hostAndPorts.stream().findFirst();
            HostAndPort hostAndPort = first.orElseGet(()->new HostAndPort(JedisSetting.local,JedisSetting.port));
            return new JedisPooled(hostAndPort,jedisSetting.buildJedisClientConfig(),jedisSetting);
        }
    },
    /** 集群 */
    cluster{
        @Override
        public UnifiedJedis createJedis(JedisSetting jedisSetting) {
            Set<HostAndPort> hostAndPorts = new HashSet<>(jedisSetting.getHostAndPorts());
            return new JedisCluster(hostAndPorts,jedisSetting.buildJedisClientConfig(),5,jedisSetting);
        }
    },
    /** 分片 */
    sharding{
        @Override
        public UnifiedJedis createJedis(JedisSetting jedisSetting) {
            List<HostAndPort> hostAndPorts = jedisSetting.getHostAndPorts();
            return new JedisSharding(hostAndPorts,jedisSetting.buildJedisClientConfig(),jedisSetting);
        }
    }
    ;

    public abstract UnifiedJedis createJedis(JedisSetting jedisSetting);
}
